#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pika

credentials = pika.PlainCredentials(username='aaa', password='111qqq!')
parameters = pika.ConnectionParameters(host='10.42.0.20', port='5672', credentials=credentials, virtual_host='mqtt')
connection = pika.BlockingConnection(parameters=parameters)
# 创建通道
channel = connection.channel()
# 创建broker
channel.exchange_declare(exchange='amq.topic', exchange_type='topic', durable=True)
# 声明一个队列，生产者和消费者都要声明一个相同的队列，用来防止万一某一方挂了，另一方能正常运行
# channel.queue_declare(queue='topic', durable=True)
# 把队列和中间人绑定
# channel.queue_bind(exchange='topic', queue='topic', routing_key='user')
# 交换机; 队列名,写明将消息发往哪个队列; 消息内容
# routing_key在使用匿名交换机的时候才需要指定，表示发送到哪个队列,注意当未定义exchange时，routing_key需和queue的值保持一致
channel.basic_publish(exchange='amq.topic', routing_key='user', body='666')
#关闭连接
connection.close()
